local ob = require("ob")
local json = require("json")

local text = [[
---
title: Example
author: John Doe
date: 2024-06-20
---

]]

local text = text.."这是一个[[wiki|链接]]和[[另一个链接]]，还有一个单独的[[wiki]]链接。"

local yaml, text = ob.extract_yaml_and_content(text)
print(yaml, text)
local nText, links = ob.processLinks(text)

print(nText)
print(json.encode(links))


print("Test2")

-- read json from all.md
local file = io.open("all.md", "r")
local content = file:read("*a")
file:close()

local jsonObj = json.decode(content)
local obAllLinkMap = {}

for i, obFile in ipairs(jsonObj) do
    local yaml, text = ob.extract_yaml_and_content(obFile.text)
    -- 替换链接
    local nText, links = ob.processLinks(text)
    -- TODO 将新文本写入 obFile.key + ".md" 文件
    -- 保存所有链接
    -- ["xx":["x1","x2"], "yy":["y1","y2"]]
    obAllLinkMap[obFile.key] = links
end

print(json.encode(obAllLinkMap))
-- 处理所有链接关系 便于前端绘制关系图谱，或查找反链
print(json.encode(ob.flattenLinks(obAllLinkMap)))